package mock_seed

import (
	"wms/internal/model"
	"wms/pkg/database"
	"wms/pkg/logger"

	"go.uber.org/zap"
)

// InitFrozenFoodCategories 初始化餐饮冻品分类数据
func InitFrozenFoodCategories() error {
	logger.Info("开始初始化餐饮冻品分类数据...")
	db := database.GetDB()

	// 检查是否已有分类数据
	var count int64
	if err := db.Model(&model.CargoCategory{}).Count(&count).Error; err != nil {
		logger.Error("检查分类数据失败", zap.Error(err))
		return err
	}

	// 如果已有数据，则先清空
	if count > 0 {
		if err := db.Exec("DELETE FROM cargo_categories").Error; err != nil {
			logger.Error("清空分类数据失败", zap.Error(err))
			return err
		}
		logger.Info("已清空原有分类数据")
	}

	// 辅助函数创建uint指针
	uintPtr := func(i uint) *uint {
		return &i
	}

	// 冷链冻品分类列表 - 首先创建一级分类
	primaryCategories := []model.CargoCategory{
		{
			Name:      "禽肉类",
			Code:      "POULTRY",
			Level:     1,
			Sort:      10,
			Status:    1,
			Remark:    "包括鸡、鸭、鹅等各类冷冻禽肉产品，主要用于餐饮行业和家庭消费的冷冻禽类食材",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "猪肉类",
			Code:      "PORK",
			Level:     1,
			Sort:      20,
			Status:    1,
			Remark:    "包括猪肉分割部位和副产品等冷冻猪肉产品，适用于餐饮、食品加工及零售行业",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "牛羊肉类",
			Code:      "BEEF_LAMB",
			Level:     1,
			Sort:      30,
			Status:    1,
			Remark:    "包括牛肉和羊肉的各种分割部位、整块肉等冷冻牛羊肉产品，适合高端餐饮和食品加工",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "海鲜水产类",
			Code:      "SEAFOOD",
			Level:     1,
			Sort:      40,
			Status:    1,
			Remark:    "包括鱼类、虾蟹类和其他海鲜等各类冷冻海鲜水产品，主要供应餐饮业和零售市场",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "预制调理类",
			Code:      "PREPARED",
			Level:     1,
			Sort:      50,
			Status:    1,
			Remark:    "包括腌制、半成品等预加工冷冻食品，适合快餐、团餐及家庭快速烹饪的便捷食材",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
	}

	// 先添加一级分类
	var primaryCategoryMap = make(map[string]uint)
	for _, category := range primaryCategories {
		if err := db.Create(&category).Error; err != nil {
			logger.Error("创建一级分类失败", zap.String("name", category.Name), zap.Error(err))
			return err
		}
		primaryCategoryMap[category.Name] = category.ID
		logger.Info("创建一级分类成功", zap.String("name", category.Name), zap.Uint("id", category.ID))
	}

	// 然后创建二级分类，并正确关联到一级分类
	secondaryCategories := []model.CargoCategory{
		// 禽肉类下的二级分类
		{
			Name:      "鸡肉",
			Code:      "CHICKEN",
			ParentID:  uintPtr(primaryCategoryMap["禽肉类"]),
			Level:     2,
			Sort:      11,
			Status:    1,
			Remark:    "包括整鸡、鸡翅、鸡腿、鸡胸等各部位冷冻鸡肉产品，富含优质蛋白，适合多种烹饪方式",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "鸭肉",
			Code:      "DUCK",
			ParentID:  uintPtr(primaryCategoryMap["禽肉类"]),
			Level:     2,
			Sort:      12,
			Status:    1,
			Remark:    "包括整鸭、鸭胸、鸭腿等冷冻鸭肉产品，肉质鲜美，适合烤制、炖煮等多种烹饪方式",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "鹅肉",
			Code:      "GOOSE",
			ParentID:  uintPtr(primaryCategoryMap["禽肉类"]),
			Level:     2,
			Sort:      13,
			Status:    1,
			Remark:    "包括整鹅、鹅胸、鹅腿等冷冻鹅肉产品，肉质细嫩，适合烧烤和炖煮",
			CreatedBy: 1,
			UpdatedBy: 1,
		},

		// 猪肉类下的二级分类
		{
			Name:      "分割部位",
			Code:      "PORK_CUTS",
			ParentID:  uintPtr(primaryCategoryMap["猪肉类"]),
			Level:     2,
			Sort:      21,
			Status:    1,
			Remark:    "包括猪肋排、五花肉、里脊等猪肉不同部位的冷冻分割产品，适用于各类烹饪需求",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "副产品",
			Code:      "PORK_BYPRODUCTS",
			ParentID:  uintPtr(primaryCategoryMap["猪肉类"]),
			Level:     2,
			Sort:      22,
			Status:    1,
			Remark:    "包括猪耳、猪尾、猪蹄等冷冻猪肉副产品，多用于特色菜肴和火锅食材",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "整块猪肉",
			Code:      "WHOLE_PORK",
			ParentID:  uintPtr(primaryCategoryMap["猪肉类"]),
			Level:     2,
			Sort:      23,
			Status:    1,
			Remark:    "整块冷冻猪肉，适合批发市场和大型餐饮企业采购",
			CreatedBy: 1,
			UpdatedBy: 1,
		},

		// 牛羊肉类下的二级分类
		{
			Name:      "牛肉",
			Code:      "BEEF",
			ParentID:  uintPtr(primaryCategoryMap["牛羊肉类"]),
			Level:     2,
			Sort:      31,
			Status:    1,
			Remark:    "包括牛排、牛腩、牛肉卷等各种规格的冷冻牛肉产品，含有丰富蛋白质和铁元素",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "羊肉",
			Code:      "LAMB",
			ParentID:  uintPtr(primaryCategoryMap["牛羊肉类"]),
			Level:     2,
			Sort:      32,
			Status:    1,
			Remark:    "包括羊排、羊腿、羊肉片等各种规格的冷冻羊肉产品，适合烧烤、涮锅等烹饪方式",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "牛羊肉制品",
			Code:      "BEEF_LAMB_PRODUCTS",
			ParentID:  uintPtr(primaryCategoryMap["牛羊肉类"]),
			Level:     2,
			Sort:      33,
			Status:    1,
			Remark:    "各类牛羊肉加工制品，如牛肉丸、羊肉卷等，方便快捷的烹饪食材",
			CreatedBy: 1,
			UpdatedBy: 1,
		},

		// 海鲜水产类下的二级分类
		{
			Name:      "鱼类",
			Code:      "FISH",
			ParentID:  uintPtr(primaryCategoryMap["海鲜水产类"]),
			Level:     2,
			Sort:      41,
			Status:    1,
			Remark:    "包括带鱼、三文鱼、巴沙鱼等各种冷冻鱼类产品，富含优质蛋白和不饱和脂肪酸",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "虾蟹类",
			Code:      "SHRIMP_CRAB",
			ParentID:  uintPtr(primaryCategoryMap["海鲜水产类"]),
			Level:     2,
			Sort:      42,
			Status:    1,
			Remark:    "包括对虾、龙虾、螃蟹等各种冷冻虾蟹类产品，高蛋白低脂肪的优质食材",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "贝类",
			Code:      "SHELLFISH",
			ParentID:  uintPtr(primaryCategoryMap["海鲜水产类"]),
			Level:     2,
			Sort:      43,
			Status:    1,
			Remark:    "包括扇贝、牡蛎、贻贝等各种冷冻贝类产品，营养丰富，适合多种烹饪方式",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "其他海鲜",
			Code:      "OTHER_SEAFOOD",
			ParentID:  uintPtr(primaryCategoryMap["海鲜水产类"]),
			Level:     2,
			Sort:      44,
			Status:    1,
			Remark:    "包括章鱼、墨鱼、海参等其他冷冻海鲜产品，具有特殊风味和营养价值",
			CreatedBy: 1,
			UpdatedBy: 1,
		},

		// 预制调理类下的二级分类
		{
			Name:      "腌制/半成品",
			Code:      "MARINATED",
			ParentID:  uintPtr(primaryCategoryMap["预制调理类"]),
			Level:     2,
			Sort:      51,
			Status:    1,
			Remark:    "预腌制或半成品冷冻食品，如腌制牛排、调味鸡翅等，便于快速烹饪",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "成品菜肴",
			Code:      "READY_MEALS",
			ParentID:  uintPtr(primaryCategoryMap["预制调理类"]),
			Level:     2,
			Sort:      52,
			Status:    1,
			Remark:    "已完全烹饪好的冷冻成品菜肴，只需加热即可食用，适合快节奏生活",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
		{
			Name:      "火锅食材",
			Code:      "HOTPOT_INGREDIENTS",
			ParentID:  uintPtr(primaryCategoryMap["预制调理类"]),
			Level:     2,
			Sort:      53,
			Status:    1,
			Remark:    "专为火锅设计的冷冻食材组合，包括肉丸、虾滑、豆制品等火锅常用食材",
			CreatedBy: 1,
			UpdatedBy: 1,
		},
	}

	// 添加二级分类
	for _, category := range secondaryCategories {
		if err := db.Create(&category).Error; err != nil {
			logger.Error("创建二级分类失败", zap.String("name", category.Name), zap.Error(err))
			return err
		}
		logger.Info("创建二级分类成功", zap.String("name", category.Name), zap.Uint("parent_id", *category.ParentID))
	}

	logger.Info("冷链冻品分类数据初始化成功")
	return nil
}

// ResetFrozenFoodCategories 重置餐饮冻品分类数据（清空后重新添加）
func ResetFrozenFoodCategories() error {
	logger.Info("开始重置餐饮冻品分类数据...")
	db := database.GetDB()

	// 清空现有分类数据
	if err := db.Exec("DELETE FROM cargo_categories").Error; err != nil {
		logger.Error("清空分类数据失败", zap.Error(err))
		return err
	}

	// 重新添加分类数据
	return InitFrozenFoodCategories()
}
